/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/**
 * @Title: ParseRetailerProduct.java
 * @Package phoenics.apeak.simple.service
 * @Description
 * @author phoenics@126com
 * @date 2016-1-30 16:01:30
 * @version V1.0
 */
package phoenics.apeak.simple.service;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import phoenics.apeak.crawl.Extractor;
import phoenics.apeak.crawl.service.DocumentPublisher;
import phoenics.apeak.simple.entity.RetailerProduct;
import phoenics.apeak.simple.store.RetailerProductDao;

// TODO: Auto-generated Javadoc
/**
 * The Class ParseRetailerProduct.
 *
 * @Description The Class ParseRetailerProduct.
 */
@Service
public class ParseRetailerProduct implements Extractor{
	
	/** The retailer product dao. */
	@Autowired
	RetailerProductDao  retailerProductDao;
	
	/** The document publisher. */
	@Autowired
	DocumentPublisher documentPublisher;
	
	/**
 * Gets the click url.
 *
 * @return the click url
 */
	public String getClickUrl() {
		return clickUrl;
	}


	/** The click url. */
	private String clickUrl;
	
	/**
 * Parses the content.
 *
 * @param content
 *            the content
 * @Description Parses the content.
 */
	private void parseContent(byte[] content)  {
		String html="";
		try {
			html = new String(content,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String [] strArray=html.split("\\r?\\n");
		String url= "";
		for(String line : strArray){
			 line=line.replaceAll("(^\\s*)|(\\s*$)","");
			 if(line.indexOf("window.location.replace") == 0){
				 String wl="window.location.replace('";
				 url = line.substring(wl.length(),line.length()-3).replace("\\","");
					break;
			 }
		}
		if(!url.equals("")){
			RetailerProduct retailerProduct=new RetailerProduct();
			retailerProduct.setUrl(url);
			retailerProduct.setClickUrl(clickUrl);
			retailerProductDao.save(retailerProduct);
		}else{
			System.out.println("RetailerProduct="+url);
		}
	}
	
	/* (non-Javadoc)
	 * @see phoenics.apeak.crawl.Extractor#setParams(java.lang.String)
	 */
	@Override
	public void setParams(String params) {
		// TODO Auto-generated method stub
		
	}
	
	/* (non-Javadoc)
	 * @see phoenics.apeak.crawl.Extractor#setUrl(java.lang.String)
	 */
	@Override
	public void setUrl(String url) {
		this.clickUrl = url;
		
	}
	
	/* (non-Javadoc)
	 * @see phoenics.apeak.crawl.Extractor#extract(byte[])
	 */
	@Override
	public void extract(byte[] content) {
		parseContent(content);
	}
}
